<?php
/**
 * Created by PhpStorm.
 * User: LYD
 * Date: 2019/1/28
 * Time: 22:45
 */
//解析类
class Parser{
    private $_tplFile; //保存模板文件的内容

    //构造方法获取模板文件的内容
    public function __construct($_tplFile)
    {
        if(!$this->_tplFile = file_get_contents($_tplFile)){
            exit("获取模板错误");
        }
    }

    //解析普通变量
    private function parVar(){
        //普通变量的正则表达式的匹配模式
        $_patten = '/\{\$([\w]+)\}/';
        //判断是否有普通变量存在
        if(preg_match($_patten,$this->_tplFile)){
            //将{$name}替换为$this->_vars['name']
            $this->_tplFile = preg_replace($_patten,"<?php echo \$this->_vars['$1']; ?>",$this->_tplFile);
        }
    }

    //解析if语句
    private function parIf(){
        $_pattenIf = '/\{if\s+\$([\w]+)\}/';
        $_pattenEndIf = '/\{\/if\}/';
        $_pattenElse = '/\{else\}/';
        if(preg_match($_pattenIf,$this->_tplFile)){
            if(preg_match($_pattenEndIf,$this->_tplFile)){
                $this->_tplFile = preg_replace($_pattenIf,"<?php if(\$this->_vars['$1']){?>",$this->_tplFile);
                $this->_tplFile = preg_replace($_pattenEndIf,"<?php } ?>",$this->_tplFile);
                if(preg_match($_pattenElse,$this->_tplFile)){
                    $this->_tplFile = preg_replace($_pattenElse,"<?php }else{ ?>",$this->_tplFile);
                }
            }else{
                exit("没有if结尾");
            }
        }
    }

    //解析foreach语句
    private function parForeach(){
        $_pattenForeach = '/\{foreach\s+\$([\w]+)\(([\w]+),([\w]+)\)\}/';
        $_pattenEndForeach = '/\{\/foreach\}/';
        $_pattenForeachValue = '/\{\$([\w]+)\[([\w]+)\]\}/';
        $_pattenForeachKey = '/\{([\w]+)\}/';
        if(preg_match($_pattenForeach,$this->_tplFile)){
            if(preg_match($_pattenEndForeach,$this->_tplFile)){
                $this->_tplFile = preg_replace($_pattenForeach,"<?php foreach(\$this->_vars['$1'] as \$$2 => \$$3){ ?>",$this->_tplFile);
                $this->_tplFile = preg_replace($_pattenEndForeach,"<?php } ?>",$this->_tplFile);
                if (preg_match($_pattenForeachKey,$this->_tplFile)) {
                    $this->_tplFile = preg_replace($_pattenForeachKey,"<?php echo \$$1 + 1?>",$this->_tplFile);
                }
                if(preg_match($_pattenForeachValue,$this->_tplFile)){
                    $this->_tplFile = preg_replace($_pattenForeachValue,"<?php echo \$$1[\"$2\"] ?>",$this->_tplFile);
                }
            }else{
                exit("foreach没有结束语句");
            }
        }
    }

    //解析include语句
    private function parInclude(){
        $_patten = '/\{include\s+file=\"([\w\.\-]+)\"\}/';
        if(preg_match($_patten,$this->_tplFile,$_ten)){
            if(!file_exists($_ten[1]) || empty($_ten)){
                exit("include引入的文件有误");
            }
            $this->_tplFile = preg_replace($_patten,"<?php include '$1' ?>",$this->_tplFile);
        }
    }

    //对外入口，通过Templates.class.php来引入
    public function compile($_parFile){
        //解析
        $this->parVar();
        $this->parIf();
        $this->parForeach();
        $this->parInclude();
        //生成编译文件
        file_put_contents($_parFile,$this->_tplFile);
    }
}
?>